﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;

namespace MINERP.DAL.UserLogin
{
    public class UserLoginDAL:DALBase
    {
        private static ObjectQuery<tb_Power> GetPower()
        {
            return DataEntites.tb_Power.Include("tb_Tech");
        }
        /// <summary>
        /// 获得用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static users GetUsers(string userName, string pwd)
        {
            #region old
            //string esql = string.Format("it.LoginName ='{0}'and it.LoginPwd ='{1}'", userName, pwd);
            //ObjectQuery<users> query = DataEntites.users.Where(esql).Include("tb_Power").Include("tb_WorkerGroup");
            //return query;
            #endregion
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                
                users user = entities.users.FirstOrDefault(u=>u.LoginName ==userName&&u.LoginPwd==pwd);
                if (user != null)
                {
                    if (!user.tb_Power.IsLoaded)
                    {
                        user.tb_Power.Load();
                    }
                    if (!user.tb_WorkerGroupReference.IsLoaded)
                    {
                        user.tb_WorkerGroupReference.Load();
                    }
                }
                return user;
            }
        }
        /// <summary>
        /// 获得程序集和窗体名
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static ObjectQuery<DbDataRecord> GetAssemblyName(string userName, string pwd)
        {
            StringBuilder esql = new StringBuilder();
            esql.Append("select ");
            esql.Append("value row(a.FrmName,a.AssemblyName) "); 
            esql.Append("from  ");
            esql.Append("newSampleYPDBEntities.users as it, ");
            esql.Append("{it.tb_Power} as a ");
            esql.Append("where  ");
            esql.Append("it.LoginName =@loginName ");
            esql.Append("and it.LoginPwd =@pwd");
            ObjectParameter name = new ObjectParameter("loginName", userName);
            ObjectParameter loginpwd = new ObjectParameter("pwd", pwd);
            ObjectQuery<DbDataRecord> query = DataEntites.CreateQuery<DbDataRecord>(esql.ToString(), name, loginpwd);
            return query;
        }

        /// <summary>
        /// 获得控件名
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static ObjectQuery<string> GetControlName(string userName, string pwd)
        {
            StringBuilder esql = new StringBuilder();
            esql.Append("select "); 
            esql.Append("value a.ControlName "); 
            esql.Append("from  ");
            esql.Append("newSampleYPDBEntities.users as it, ");
            esql.Append("it.tb_Action as a ");
            esql.Append("where  ");
            esql.Append("it.LoginName =@loginName ");
            esql.Append("and it.LoginPwd =@pwd");
            ObjectParameter name = new ObjectParameter("loginName", userName);
            ObjectParameter loginpwd = new ObjectParameter("pwd", pwd);
            ObjectQuery<string> query = DataEntites.CreateQuery<string>(esql.ToString(), name, loginpwd);
            return query;
        }
        /// <summary>
        /// 获得节点
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static ObjectQuery<DbDataRecord> GetTechs(string userName, string pwd)
        {
            StringBuilder esql = new StringBuilder();
            esql.Append("select ");
            esql.Append("value row(b.TechName,b.TechId)  ");
            esql.Append("from  ");
            esql.Append("newSampleYPDBEntities.users as it, ");
            esql.Append("{it.tb_Power} as a, ");
            esql.Append("a.tb_Tech as b ");
            esql.Append("where  ");
            esql.Append("it.LoginName =@loginName ");
            esql.Append("and it.LoginPwd =@pwd");
            ObjectParameter name = new ObjectParameter("loginName", userName);
            ObjectParameter loginpwd = new ObjectParameter("pwd", pwd);
            ObjectQuery<DbDataRecord> query = DataEntites.CreateQuery<DbDataRecord>(esql.ToString(), name, loginpwd);
            return query;
        }

        /// <summary>
        /// 获得所有角色列表
        /// </summary>
        /// <returns></returns>
        public static List<tb_Power> GetPowerList()
        {
            List<tb_Power> list = new List<tb_Power>();
            foreach (var power in GetPower())
            {
                list.Add(power);
            }
            return list;
        }

        /// <summary>
        /// 得到用户的角色列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static List<tb_Power> GetPowerByUserID(int userID)
        {
            List<tb_Power> userPowerList = new List<tb_Power>();

            List<tb_Power> list = GetPowerList();

            ObjectQuery<users> curr_users = DataEntites.users.Where(string.Format("it.userid = {0}",userID));
            if (curr_users.Count<users>() > 0)
            {
                users tmpUser = curr_users.First();

                foreach (tb_Power item in list)
                {
                    if (item.users.Contains(tmpUser))
                    {
                        userPowerList.Add(item);
                    }
                }
            }

            return userPowerList;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inUserID"></param>
        /// <returns></returns>
        public static Dictionary<int,string>GetTechFormNam(int inUserID)
        {
            Dictionary<int,string> techName=new Dictionary<int, string>();
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                users user = db.users.FirstOrDefault(p => p.userID == inUserID);
                
                user.tb_Power.Load();
                List<tb_Power> powerList = user.tb_Power.ToList();
                foreach (tb_Power tbPower in powerList)
                {
                    tbPower.tb_Tech.Load();
                    List<tb_Tech> tech = tbPower.tb_Tech.ToList();
                    foreach (tb_Tech list in tech)
                    {
                        if (list != null)
                        {
                            if (!techName.Keys.Contains(list.TechId))
                            {
                                techName.Add(list.TechId, list.FrmName);
                            }
                           
                        }  
                    }
                    
                }
            }
            return techName;
        }
    }
}
